#!usr/bin/env python
# -*- coding:utf-8 -*-
# user nanme: Mr.zhang
# conf time : 2022/2/9   10:19 上午
# project   : stop_pkg.py
# IDE       : PyCharm
# email     :17600960626@163.com
import logging
import os
import subprocess



# apk 字典
def apk_dict():
    apk_name = []
    apk = {
        "ikey": "com.emoji.ikeyboard",
        "ikeyd": "com.emoji.ikeyboard.debug",
        "gb": "com.google.android.inputmethod.latin",
        "s": "com.touchtype.swiftkey",
        "touchpal": "com.cootek.smartinputv5",
        "facemoji": "com.simejikeyboard",
        "indic": "com.kikatech.keyboard.emoji.indian.debug",
        "oem": "com.kikaoem.hw.qisiemoji.inputmethod",
        "kika": "com.qisiemoji.inputmethod.debug",
        "huawei": "com.hw.inputmethod",
        "z": "com.huawei.ohos.inputmethod",
        "pro": "com.emoji.coolkeyboard",
        "prod": "com.emoji.coolkeyboard.debug",
        "prot": "kika.emoji.keyboard.teclados.clavier.debug",
        "xp": "com.kikatech.hackkeyboardxposedmodule",
        "prohw": "com.emoji.coolkeyboard.hw.store",
        "vivo": "com.kikaoem.vivo.qisiemoji.inputmethod",
        "hack": "com.kikatech.hackkeyboardxposedmodule"
    }
    apklist = input('请输入进程简称：')
    for key in apk:
        if key == apklist:
            apkname = apk[key]
            apk_name.append(apkname)
            logging.warning('进程名称:%s ' % apkname)
            break
    else:
        logging.error('包名简称不存在请重新执行')
        return apk_dict()
    return apk_name


def Device(Devicecmd):# 获取设备ip地址并连接
    '''
    初始化设备
    :param Devicecmd:cmd命令
    :return:
    '''
    device_str = subprocess.getoutput(Devicecmd)
    device_list = []
    m = (device_str.strip()).split('\n')
    for line in m[1:]:
        if 'List' not in line:
            device_list.append(line.split('\t')[0])
    print('已连接设备数量%s台'%len(device_list),"\n设备devicesID列表:",device_list)
    return device_list


# 批量kill
def apk_kill(deviceslist):
    '''
    :param deviceslist: device列表
    :return:
    '''
    equipment_name = Device(deviceslist)
    apk_name = ['com.huawei.ohos.inputmethod','com.touchtype.swiftkey','com.google.android.inputmethod.latin']

    for i in equipment_name:
        rm1 = ('adb -s %s shell rm /sdcard/word_input'%i)
        res_rm1 = subprocess.Popen(rm1,stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True,
                                   universal_newlines=True)

        rm2 = ('adb -s %s shell rm /sdcard/word_output'%i)

        res_rm2 = subprocess.Popen(rm2,stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True,
                                   universal_newlines=True)


        print(res_rm1.stdout.read(),res_rm2.stdout.read())
        for apk in apk_name:
            rm3 = ('adb -s %s shell su -c rm /data1/data1/%s/files/word_output'%(i,apk))
            res_rm3 = subprocess.Popen(rm3, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True,
                                       universal_newlines=True)
            print(res_rm3.stdout.read())
            # unins = 'adb -s %s shell su -c ps|grep %s' % (i, apk)
            if apk =='com.google.android.inputmethod.latin':
                un_gboard = "adb -s %s  shell ps | grep 'com.google.android.inputmethod.latin' | awk '{print $2}'"%(i)
                res_gbord =subprocess.Popen(un_gboard, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True,
                                   universal_newlines=True)
                res_gbords = res_gbord.stdout.read()
                kill_apk = ("adb -s %s shell su -c kill %s" % (i, res_gbords))
                res_kill = subprocess.run(kill_apk, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True,
                                          universal_newlines=True)
                code = res_kill.returncode
                if code == 0:
                    logging.warning('设备：%s 进程 %s kill完成\n' % (i, apk))
                else:
                    logging.error('设备%s,kill失败' % i)
                    # return apk_kill
            else:

                unins=("adb -s %s shell su -c ps | grep %s | awk '{print $2}'" % (i,apk))
                res = subprocess.Popen(unins, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True,
                                       universal_newlines=True)
                res_list=res.stdout.read()

                kill_apk=("adb -s %s shell su -c kill %s"%(i,res_list))
                res_kill = subprocess.run(kill_apk,stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True,
                                       universal_newlines=True)
                code=res_kill.returncode
                if code == 0:
                    logging.warning('设备：%s 进程 %s kill完成\n' %(i,apk))
                else:
                    logging.error('设备%s,kill失败'%i)
                    # return apk_kill

if __name__ == '__main__':
    apk_kill(deviceslist='adb devices')
